<?php
/**
 * 应用程序配置操作类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CConf.php 2010-05-21 15:18:51Z andery $
 */
class CConf 
{
    private static $_config = null;
    
    public function __construct($config)
    {
        // 扩展配置
        if (isset($config['extconf']) && is_array($config['extconf'])) {
            foreach ($config['extconf'] as $key=>$extconf) {
                $config[$key] = file_exists($extconf) ? include($extconf) : null;
            }
        }

        self::$_config = $config;
    }
    
    public static function getInstance($config)
    {
        if (self::$_config == null) {
            new CConf($config);
        } else {
            return self::$_config;
        }
    }
    
    /**
     * 获取指定模块配置信息
     * @param string 配置信息键值，支持多级读取，用"."指向下级
     */
    public static function get($confkeys='all')
    {
        if ($confkeys=='all') {
            return self::$_config;
        } else {
            //拆分为数组
            $keys = explode('.', $confkeys);
            $_config_temp = self::$_config;
            foreach ($keys as $key) {
                if(!array_key_exists($key, $_config_temp)) {
                    return null;
                }else{
                    $_config_temp = $_config_temp[$key];
                }
            }
            return $_config_temp;
        }
    }
    
    /**
     * 设置指定模块配置信息     * 一般作为应用程序运行后对框架配置进行修改     * 
     * @param string 配置信息键值
     * @param mixed  传递此参数则修改相应键的配置信息值
     */
    public static function set($confkeys, $cvalue='')
    {        self::$_config[$confkeys] = $cvalue;
    }
    
    public function __clone(){}
}